package pl.edu.pk.mech.kkbus.service;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import org.springframework.stereotype.Service;

import pl.edu.pk.mech.kkbus.component.MyEntityManagerFactory;
import pl.edu.pk.mech.kkbus.entities.News;

@Service
public class NewsService {
    
    EntityManagerFactory emf = MyEntityManagerFactory.get();
    
    public List<pl.edu.pk.mech.kkbus.dto.News> getAllNews() {
        List<pl.edu.pk.mech.kkbus.dto.News> result = 
                new LinkedList<pl.edu.pk.mech.kkbus.dto.News>();
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            // Bez sorotwania.
            Query query = em.createQuery("SELECT n FROM News n");
            List<News> results = (List<News>) query.getResultList();
            Iterator<News> iterator = results.iterator();
            while (iterator.hasNext()) {  
                News news = iterator.next();
                
                result.add(new pl.edu.pk.mech.kkbus.dto.News(news.getKey().getId(),
                        news.getTitle(), news.getContent(), news.getDate()));
            }   
            return result;
        } catch(NoResultException e) {
            return null;
        } finally {
            if (em != null)
                em.close();
        }
    } 
    
    public boolean create(pl.edu.pk.mech.kkbus.dto.News n) {
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            // News
            News news = new News(n.getTitle(), n.getContent(), n.getDate());
            em.persist(news);
            
            return true;
        } catch(Exception e) {
            return false;
        } finally {
            if (em != null)
                em.close();
        }
    }
    
    public boolean update(long id, pl.edu.pk.mech.kkbus.dto.News n) {
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            News news = em.find(News.class, new Long(id));
            news.setContent(n.getContent());
            news.setTitle(n.getTitle());
            if (n.getDate() != null) {
                news.setDate(n.getDate());
            }
            em.persist(news);
            
            return true;
        } catch(Exception e) {
            return false;
        } finally {
            if (em != null)
                em.close();
        }
    }
    
    public boolean delete(long id) {
        EntityManager em = null;        
        try {
            em = emf.createEntityManager();
            News news = em.find(News.class, new Long(id));
            em.remove(news);            
            return true;
        } catch(Exception e) {
            return false;
        } finally {
            if (em != null)
                em.close();
        }
    }
}
